import { defineStore } from 'pinia'
// 引入请求接口
import { reqLogin } from '@/api/user'
// 引入数据类型
import type { LoginFormData } from '@/api/user/type'
let useUserStore = defineStore('User', {
  // state存储数据
  state: () => {
    return {
      token: localStorage.getItem('token'),
    }
  },
  // 异步逻辑的地方
  actions: {
    // 用户登录的方法
    async userLogin(data: LoginFormData) {
      let result: any = await reqLogin(data)
      if (result.code == 200) {
        this.token = result.data.token
        // 持久化
        localStorage.setItem('token', result.data.token)
        return 'ok'
      } else {
        return Promise.reject(new Error(result.data as string))
      }
    },
  },
  getters: {},
})

export default useUserStore
